<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="System.Text" %>

<script runat="server">
    public void Page_Load()
    {
        Response.Clear();
        Response.ContentType = "text/xml";

        try
        {
            Response.Write(
                GetRSSFromListView(
                Request.QueryString["List"],
                Request.QueryString["View"],
                Request.QueryString["Title"]
                ));
        }
        catch (Exception ex)
        {
            Response.Write("<pre>");
            Response.Write(ex.ToString());
            Response.Write("</pre>");
        }
    }

    private static string GetRSSFromListView(string strListGUID, string strViewGUID, string strTitle)
    {
        SPListItemCollection items = null;

        StringBuilder sb = new StringBuilder();
        sb.Append("<?xml version=\"1.0\"?>");
        sb.Append("<rss version=\"2.0\">");

        Guid listGuid = new Guid(
            HttpContext.Current.Server.UrlDecode(strListGUID));

        SPSite site = SPContext.Current.Site;
        SPWeb web = site.OpenWeb();
        SPList list = web.Lists[listGuid];

        SPView view = null;
        if (strViewGUID != null)
        {
            Guid viewGuid = new Guid(
                HttpContext.Current.Server.UrlDecode(strViewGUID));
            view = list.Views[viewGuid];
        }
        else
        {
            view = list.DefaultView;
        }

        if (strTitle == null)
        {
            items = list.GetItems(view);
        }
        else
        {
            SPQuery query = new SPQuery();
            query.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + strTitle + "</Value></Eq></Where>";
            items = list.GetItems(query);
        }

        sb.Append("<channel>");
        AddTag("title", list.Title, sb);
        AddTag("description", list.Description, sb);

        // I have the items, lets start spewing out the RSS.
        foreach (SPListItem item in items)
        {
            sb.Append("<item>");
            AddTag("title", item["LinkTitle"].ToString(), sb);
            AddTag("link", list.RootFolder.Url + "/DispForm.aspx?ID=" + item.ID, sb);
            sb.Append("<description>");

            foreach (string viewField in view.ViewFields)
            {
                if (viewField != "LinkTitle")
                {
                    AddTag(
                        viewField.Replace("_x0020_", " "),
                        item.GetFormattedValue(viewField),
                        sb);
                }
            }

            bool isEditable = false;

            foreach (SPRoleDefinition rd in item.AllRolesForCurrentUser)
            {
                isEditable = isEditable |
                    ((rd.BasePermissions & SPBasePermissions.EditListItems) == SPBasePermissions.EditListItems);
            }
            try
            {
                SPUser objUser = SPContext.Current.Web.CurrentUser;
                if (item.DoesUserHavePermissions(objUser, Microsoft.SharePoint.SPBasePermissions.EditListItems))
                {
                    isEditable = true;
                }
            }
            catch (Exception ex)
            {
            }

            AddTag("IsEditable", isEditable.ToString(), sb);

            sb.Append("</description>");
            sb.Append("</item>");
        }

        sb.Append("</channel>");
        sb.Append("</rss>");
        return sb.ToString();
    }

    private static void AddTag(string tagText, string tagValue, StringBuilder sb)
    {
        sb.Append("<");
        sb.Append(tagText.Replace(" ", "_"));
        sb.Append("> <![CDATA[");
        sb.Append(tagValue);
        sb.Append("]]> </");
        sb.Append(tagText.Replace(" ", "_"));
        sb.Append(">");
    }
</script>